<?php
namespace Common\Model;

/**
* 微信用户
* @date: 2018年3月19日 上午10:42:21
* @author: liuyang
*/
class WeixinMemberModel  extends BaseModel { 
    
    const MEMBER_TYPE=1;
    const SALES_TYPE=2;
    
    /**
     * 查询
     * @param unknown $unionId
     */
    public function getByUnionId($unionId){
        $where  = array();
        $where['unionid']=$unionId;
        return $this->where($where)->find();
    }
    
    /**
     * 查询
     * @param unknown $unionId
     */
    public function getByOpenId($openId){
        $where  = array();
        $where['openid']=$openId;
        $where['user_type']=self::SALES_TYPE; 
        return $this->where($where)->find();
    }
     
     
    /**
    *
    * 更新或者添加
    * @date: 2018年3月19日 下午3:47:13
    * @author: liuyang
    * @param: variable
    * @return:
    */
     public function addOrUpdate($unionId,$data){
         $obj = $this->getByUnionId($unionId);
         if($obj['id']>0 && $unionId != null){
             $where = array();
             $where['unionid']=$unionId;
             $data['login_time']=time();
             return $this->where($where)->save($data);
         }else{
             $data['createdate']=time();
             return  $this->add($data);
         } 
     }
     /**
      * 更新关注的页面
      * @param  $unionId
      * @param  $data
      */
     public function updateFollowData($unionId,$data){
         $data = $this->getFieldByList($data, array("subscribe_time","remark","groupid","tagid_list","nickname","sex","province","city","headimgurl"));
         $data['tagid_list']=json_encode(tagid_list);
         $where  = array();
         $where['unionid']=$unionId;
         return $this->where($where)->save($data);
     }
     /**
      * 根据ID集合获取用户列表
      * @param unknown $userId
      * @return \Think\mixed
      */
     public function getListByUserIds($userId){
         $where = array();
         $where['user_id']=array("IN",$userId);
         $where['user_type']=self::MEMBER_TYPE;
         $options=array();
         $options['field']="nickname,pic";
         return $this->where($where)->select();
     }
     /**
      * 获取单条信息
      * @param unknown $userId
      * @return \Think\mixed
      */
    public function getByUserId($userId){
         $where = array();
         if(is_array($userId)){
             $where['user_id']=array("IN",$userId);
         }else{
             $where['user_id']=$userId;
         }
         $where['user_type']=self::MEMBER_TYPE;
         $options=array();
         $options['field']="openid,nickname,pic,sex";
         return $this->where($where)->find();
     }
     
     /**
      * 获取单条信息
      * @param unknown $userId
      * @return \Think\mixed
      */
     public function getBySalesId($userId){
         $where = array();
         $where['user_id']=array("IN",$userId);
         $where['user_type']=self::SALES_TYPE;
         $options=array();
         $options['field']="openid,nickname,pic";
         return $this->where($where)->find();
     }
    
}
	 